package cn.xd.core.response;

import cn.xd.common.exception.BizException;
import cn.xd.core.exception.DBException;

/**
 * @author : mirzaLin
 * @data : 2023/11/19
 * @description : DB整型结果
 **/
public class IntegerResult {
    private final Integer res;

    private IntegerResult(Integer res) {
        this.res = res;
    }

    public static IntegerResult of(Integer res) {
        return new IntegerResult(res);
    }

    public void failThrow(RuntimeException exception) throws RuntimeException {
        if (res == 0) {
            throw exception;
        }
    }

    public void failThrow(String msg) throws BizException {
        failThrow(new BizException(msg));
    }

    public void failThrow() {
        failThrow(new DBException("db异常"));
    }

    public void fail(Function consumer) {
        if (res == 0) {
            consumer.execute();
        }
    }

    public void success(Function consumer) {
        if (res > 0) {
            consumer.execute();
        }
    }

}
